import request from "@/utils/request";
import { downloadFileByData } from "@/utils/download";

/**
 * 报名审批 实体类型定义
 * 
 * @author 17607080935
 * @date 2025-07-19 11:37:26
 */
export interface ApprovalSignModel {
  /** 主键ID */

  /** 客户id */
  customerId ?: string;

  /** 学生id */
  studentId ?: string;

  /** 合同编号 */
  contractCode ?: string;

  /** 业绩归属 */
  performanceBelonging ?: string;

  /** 状态 */
  status ?: string;

  /** 附件 */
  enclosureJson ?: string;

  /** 课程id */
  courseId ?: string;

  /** 课程金额 */
  coursePrice ?: number;

  /** 课程课时 */
  courseNumber ?: number;

  /** 优惠金额 */
  discountPrice ?: number;

  /** 赠送课时 */
  freeNumber ?: number;

  /** 备注 */
  remark ?: string;

  /** 付款金额 */
  paymentPrice ?: number;

  /** 付款方式 */
  paymentMethod ?: number;

  /** 付款类型 */
  paymentType ?: number;

  /** 审批类型 */
  type ?: number;

  /** 付款备注 */
  paymentRemark ?: string;

  /** 唯一主键 */
  id ?: string;

  /** 父级主键集合 */
  orgIds ?: string;

  /** 修改人 */
  updateBy ?: string;

  /** 创建时间 */
  createTime ?: string;

  /** 创建者 */
  createBy ?: string;

  /** 修改时间 */
  updateTime ?: string;

  /** 乐观锁 */
  version ?: number;


}

/**
 * 分页查询参数类型
 */
export interface ApprovalSignQueryParams {
  /** 页码 */
  pageNo ?: number;

  /** 每页条数 */
  pageSize ?: number;

  /** 客户id */
    customerId_EQ ?: string;
  /** 学生id */
    studentId_EQ ?: string;
  /** 合同编号 */
    contractCode_EQ ?: string;
  /** 业绩归属 */
    performanceBelonging_LIKE ?: string;
    /** 状态 */
    status_EQ ?: string;
  /** 课程id */
    courseId_EQ ?: string;

  /** 排序字段 */
  orderBy ?: string;

  /** 排序方式 */
  sortOrder ?: 'asc' | 'desc';
}

/**
 * API 响应类型
 */
export interface ApiResponse<T = any> {
  code: number;
  msg: string;
  data: T;
  timestamp: number;
}

/**
 * 分页响应数据类型
 */
export interface PageResult<T> {
  rows: T[];
  total: number;
  size: number;
  current: number;
  pages: number;
}

/**
 * 获取报名审批分页列表
 * @param params 查询参数
 * @returns Promise<ApiResponse<PageResult<ApprovalSignModel>>>
 */
export function getList(params: ApprovalSignQueryParams): Promise < ApiResponse < PageResult <ApprovalSignModel >>> {
  return request({
    url: "/api/v1/approval/sign/findPage",
    method: "get",
  params,
});
}

/**
 * 新增报名审批
 * @param data 新增数据
 * @returns Promise<ApiResponse<ApprovalSignModel>>
 */
export function doInsert(data: Partial<ApprovalSignModel >): Promise < ApiResponse <ApprovalSignModel >> {
  return request({
    url: "/api/v1/approval/sign/insert",
    method: "post",
  data,
});
}

/**
 * 更新报名审批
 * @param data 更新数据
 * @returns Promise<ApiResponse<ApprovalSignModel>>
 */
export function doUpdate(data: ApprovalSignModel): Promise < ApiResponse <ApprovalSignModel >> {
  return request({
    url: "/api/v1/approval/sign/update",
    method: "post",
  data,
});
}

/**
 * 删除报名审批
 * @param params 删除参数
 * @returns Promise<ApiResponse<boolean>>
 */
export function doDelete(params: { id: string }): Promise<ApiResponse<boolean>> {
  return request({
  url: "/api/v1/approval/sign/del",
  method: "post",
    params,
  });
}

/**
 * 批量删除报名审批
 * @param params 批量删除参数
 * @returns Promise<ApiResponse<boolean>>
 */
export function doDeleteAll(params: { ids: string }): Promise<ApiResponse<boolean>> {
  return request({
  url: "/api/v1/approval/sign/delAll",
  method: "post",
    params,
  });
}

/**
 * 导出Excel 目前只支持一层参数传递
 * @param params 导出参数
 * @returns Promise<void>
 */
export async function doExportExcel(params: ApprovalSignQueryParams): Promise < void> {
  const authURL = "/api/v1/approval/sign/excel/auth/export";

  const downloadURL = "/api/v1/approval/sign/excel/export/";

  try {
    // 认证
    const response: ApiResponse<string> = await request({
      url: authURL,
      method: "get",
      params,
    });

    if(response.data) {
  // 下载文件
  downloadFileByData(downloadURL + response.data, params);
}
  } catch (error) {
  console.error('导出Excel失败:', error);
  throw error;
}
}

/**
 * 下载导入模版
 * @returns Promise<void>
 */
export async function doDownloadTemplate(): Promise<void> {
  const authURL = "/api/v1/approval/sign/excel/auth/import-template-export";

  const downloadURL = "/api/v1/approval/sign/excel/export/";

  try {
    // 认证
    const response: ApiResponse<string> = await request({
      url: authURL,
      method: "get",
    });

    if (response.data) {
      // 下载文件
      downloadFileByData(downloadURL + response.data, {});
    }
  } catch (error) {
    console.error('下载模板失败:', error);
    throw error;
  }
}

/**
 * 导入Excel
 * @param data 上传的文件数据
 * @returns Promise<ApiResponse<any>>
 */
export function doImportExcel(data: FormData): Promise<ApiResponse<any>> {
  return request({
  url: "/api/v1/approval/sign/importExcel",
  method: "post",
    // 最长超时时间 3 分钟
    timeout: 180000,
      headers: {
    "Content-Type": "multipart/form-data"
  },
  data,
  });
}

/**
 * 获取报名审批详情
 * @param id 主键ID
 * @returns Promise<ApiResponse<ApprovalSignModel>>
 */
export function getById(id: string): Promise<ApiResponse<ApprovalSignModel >> {
  return request({
    url: `/api/v1/approval/sign/findById/${id}`,
method: "get",
  });
}

/**
 * 启用/禁用报名审批
 * @param params 启用禁用参数
 * @returns Promise<ApiResponse<boolean>>
 */
export function doEnable(params: { id: string; enable: boolean }): Promise<ApiResponse<boolean>> {
  return request({
  url: "/api/v1/approval/sign/enable",
  method: "post",
    params,
  });
}